package com.example;

import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * Description:客户端接收响应数据
 * User: bjc
 * Date: 2025-05-30
 * Time: 20:39
 */
@RestController
public class ResponseController {
    /**
     * HttpServletResponse提供的方法
     * @param response
     */
    @RequestMapping("/response")
    public void response(HttpServletResponse response) {
        //1.设置响应状态码
        response.setStatus(401);
        //2.设置响应头
        response.setHeader("age","18");
        response.setHeader("name","zhangsan");
        //3.设置响应体
        response.setContentType("text/html;charset=utf-8");
        try {
            response.getWriter().write("<h1>响应体</h1>");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Spring中提供的方法-ResponseEntity
     * @param response
     * @return
     * @throws IOException
     */
    @RequestMapping("/response2")
    public ResponseEntity<String> response2(HttpServletResponse response) throws IOException {
        return ResponseEntity
                .status(401)
                .header("name","bjc")
                .body("<h1>hello response</h1>");
    }
}
